// pages/inpram/drug/in_StockInquiry/in_StockInquiry.js
import { $wuxToptips } from '../../../../dist/index'
const network = require('../../../../utils/network.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value1:'',
 
    displayValue1: '请选择',
    //选择库存地点之后的参数
    pramLocation: {
      deptId: '',
      deptCode: '',
      deptName: ''
    },
    locationList:[],
    drugList:[],
    drugLocationList:[]
  },

  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    var that = this
    //调用封装好的网络请求，获取数据
    network.get('/utils/drugdept', {}
    ).then(function (res) {
      console.log('res:', res)
      var obj1 = res.data.data

      that.setData({
        'locationList': obj1,
      })
    }).then(function (res) {

    }).catch(function (res) {
      console.log(res)
    })


  },

  requestStockItem: function (deptId){
    var that = this
    //调用封装好的网络请求，获取数据
    network.get('/utils/drug?locId='+deptId, {}
    ).then(function (res) {
      console.log('res:', res)
      var obj1 = res.data.data
      //console.log('obj1:', obj1.[0 … 99])
     /* var obj2=new Array;
      for (var j = 0; j < 20; j++) {
        obj2[j] = obj1[j];
      }
      console.log('obj2:', obj2)*/
      /*
      var a = res.data.data;
      var _listData = Object.values(a.reduce((re, item) => {
        re[item.receiveTime] ? re[item.receiveTime].push(item) : re[item.receiveTime] = [item];
        return re;
      }, {}));*/
      
      that.setData({
        'drugList': obj1,
      
      })
    }).then(function (res) {

    }).catch(function (res) {
      console.log(res)
    })
  },

  bindPickerChange_location: function (e) {
    let that = this;
    var nowData = [];
    nowData = that.properties.locationList;//当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
    var nowIdx = e.target.dataset.index;//当前点击的索引
    // var nowText = nowData[nowIdx].text;//当前点击的内容
    //console.log('picker数组为', nowData);
    //console.log('picker发送选择改变，携带值为', e.detail.value);
    that.setData({   //给变量赋值
      //dept_index: e.detail.value,  //每次选择了下拉列表的内容同时修改下标然后修改显示的内容，显示的内容和选择的内容一致
      dept_select: true,
      'pramLocation.deptId': nowData[e.detail.value].deptId,
      'pramLocation.deptCode': nowData[e.detail.value].deptCode,
      'pramLocation.deptName': nowData[e.detail.value].deptName

    })
    that.requestStockItem(nowData[e.detail.value].deptId);
  },


  bindPickerChange_doctor: function (e) {
    let that = this;
    var nowData = [];
    nowData = that.properties.doctor_list;//当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
    var nowIdx = e.target.dataset.index;//当前点击的索引
    // var nowText = nowData[nowIdx].text;//当前点击的内容
    //console.log('picker数组为', nowData);
    //console.log('picker发送选择改变，携带值为', e.detail.value);
    that.setData({   //给变量赋值
      //dept_index: e.detail.value,  //每次选择了下拉列表的内容同时修改下标然后修改显示的内容，显示的内容和选择的内容一致
      doctor_select: true,
      'pram_doctor.doctorid': nowData[e.detail.value].userId,
      'pram_doctor.doctorcode': nowData[e.detail.value].userCode,
      'pram_doctor.doctorname': nowData[e.detail.value].userName

    })
    console.log('自定义值:', that.properties.pram_doctor);
  },

  setValue(values, key) {
    this.setData({
      [`value${key}`]: values.value,
      [`displayValue${key}`]: values.label,
    })
  },


  onConfirm(e) {
    const { index } = e.currentTarget.dataset
    this.setValue(e.detail, index)
    console.log(`onConfirm${index}`, e.detail)
    this.data.drugLocationList = e.detail.value

  },
  onValueChange(e) {
    const { index } = e.currentTarget.dataset
    console.log(`onValueChange${index}`, e.detail)
  },
  onVisibleChange(e) {
    this.setData({ visible: e.detail.visible })
    
    if (String(e.detail.visible) == 'true'){
    wx.showLoading({
      title: '加载中',
    })
    }
    setTimeout(function () {
     wx.hideLoading();
    }, 2000)
  },
  onClick() {
    this.setData({ visible: true })
    console.log(`1111111`)
  },
  /**
   * 检查参数是否合法，跳转
   */
  yesBtn:function(){
    console.log(this.data)
    
    if(this.data.pramLocation.deptId==""){
      $wuxToptips().error({
        hidden: false,
        text: '库存地点不能为空',
        duration: 2000,
        success() { },
      })
      
    } else if (this.data.drugLocationList.length==0){
      $wuxToptips().error({
        hidden: false,
        text: '库存项目不能为空',
        duration: 2000,
        success() { },
      })

    }else{
    wx.navigateTo({
      url: '../../../view/drug/tableStock/tableStock?locId=' + this.data.pramLocation.deptId + '&drugList=' + this.data.drugLocationList,
    })
    }
  }
})